package com.cqut.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ConnectDB {
	private  Connection con=null;
	public  Connection getConnection(){
		String url="jdbc:mysql://localhost:3306/student";
		String name="root";
		String password="root";
		String driverName="com.mysql.jdbc.Driver";
		try {
			Class.forName(driverName);
			if(con==null){
				con=DriverManager.getConnection(url, name, password);
			}
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return con;
	}
	
	public static void closeConnection(Connection con){
		Boolean b=false;
		try {
			b=con.isClosed();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		if(con!=null&&b==false){
			try {
				con.close();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
	}
    
	public static void closeStatement(PreparedStatement s){
		Boolean b=false;
		try {
			b=s.isClosed();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		if(s!=null&&b==false){
			try {
				s.close();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		
	}
	
	public static void coseAll(Connection con,PreparedStatement s){
		closeStatement(s);
		closeConnection(con);
	}
}
